package com.example.city_home_app.RecyclerViewAdapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.city_home_app.Activity.BaseActivity;
import com.example.city_home_app.Activity.MomentInfoActivity;
import com.example.city_home_app.Activity.PhotoViewActivity;
import com.example.city_home_app.Bean.Down_MomentComment;
import com.example.city_home_app.Bean.MomentImg;
import com.example.city_home_app.R;
import com.example.city_home_app.UI.MyCircleImageView;
import com.example.city_home_app.UI.UIUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Adapter_MomentComment extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<Down_MomentComment> list;
    private FragmentManager fragmentManager;
    private int userid;
    private String username;

    public Adapter_MomentComment(Context context, FragmentManager fragmentManager) {
        this.context = context;
        this.fragmentManager = fragmentManager;
    }

    public void setList(List<Down_MomentComment> list) {
        this.list = list;
    }

    public void setUserid(int userid) {
        this.userid = userid;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.recitem_moment_comment, parent, false);
        Adapter_MomentComment.ViewHolder viewHolder = new Adapter_MomentComment.ViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final Down_MomentComment bean = list.get(position);
        if (bean == null) {
            return;
        }
        final Adapter_MomentComment.ViewHolder viewHolder = (Adapter_MomentComment.ViewHolder) holder;

        if(bean.getComment_isreply()==2){
            viewHolder.tvRecitemMomentcommentAuthor.setText(username+" 回复 "+bean.getUser_name());
        }else {
            viewHolder.tvRecitemMomentcommentAuthor.setText(bean.getUser_name());
        }
        viewHolder.tvRecitemMomentcommentContent.setText(bean.getComment_content());
        Picasso.with(context)
                .load("http://siminzhang.top:8088/city_home_server/public/static/posters/"+bean.getUser_headimage())
                .into(viewHolder.imgRecitemMomentcommentHead);
        if(!bean.getComment_pic().equals("")){
            viewHolder.tvRecitemMomentcommentSeeimg.setVisibility(View.VISIBLE);
        }else{
            viewHolder.tvRecitemMomentcommentSeeimg.setVisibility(View.GONE);
        }

        viewHolder.tvRecitemMomentcommentSeeimg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<String> imglist = new ArrayList<>();
                imglist.add(bean.getComment_pic());
                Intent intent = new Intent(context, PhotoViewActivity.class);
                intent.putStringArrayListExtra("urls",imglist);
                intent.putExtra("currentPosition", 0);
                context.startActivity(intent);
            }
        });

        viewHolder.tvRecitemMomentcommentAuthor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(BaseActivity.getUserinfo().getUserid()==userid&&bean.getComment_isreply()==1&&bean.getUser_id()!=userid){
                    MomentInfoActivity.instance.showcommentDialog(2,bean.getUser_id());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_recitem_momentcomment_head)
        MyCircleImageView imgRecitemMomentcommentHead;
        @BindView(R.id.tv_recitem_momentcomment_author)
        TextView tvRecitemMomentcommentAuthor;
        @BindView(R.id.tv_recitem_momentcomment_content)
        TextView tvRecitemMomentcommentContent;
        @BindView(R.id.tv_recitem_momentcomment_seeimg)
        TextView tvRecitemMomentcommentSeeimg;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
